<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['device:deviceOfflineTask:add']"
        >{{$t('common.add')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['device:deviceOfflineTask:remove']"
        >{{$t('common.delete')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleOfflineTaskConfig"
          v-hasPermi="['device:deviceOfflineTask:remove']"
        >{{$t('deviceDetail.offlineTask.sendTask')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceOfflineTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('deviceDetail.offlineTask.type')" align="center" prop="taskType" />
      <el-table-column :label="$t('deviceDetail.offlineTask.monitorFlag')" align="center" prop="flag" />
      <el-table-column :label="$t('deviceDetail.offlineTask.executeDate')" align="center" prop="date" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.date, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('deviceDetail.offlineTask.executeTime')"  align="center" prop="datetime" />
      <el-table-column :label="$t('deviceDetail.offlineTask.dataType')" align="center" prop="dataType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_data_type" :value="scope.row.dataType"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('deviceDetail.offlineTask.slave')" align="center" prop="slaveAddr" />
      <el-table-column :label="$t('deviceDetail.offlineTask.funcCode')" align="center" prop="funcCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_func_code" :value="scope.row.funcCode"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('deviceDetail.offlineTask.dataAddress')" align="center" prop="dataAddr" />
      <el-table-column :label="$t('deviceDetail.offlineTask.setValue')" align="center" prop="dataValue" />
      <el-table-column :label="$t('deviceDetail.offlineTask.dataChange')" align="center" prop="dataChange">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_enabled" :value="scope.row.dataChange"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('deviceDetail.offlineTask.Threshold')" align="center" prop="threshold">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_enabled" :value="scope.row.threshold"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('deviceDetail.offlineTask.endEnabled')" align="center" prop="endEnabled">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_enabled" :value="scope.row.endEnabled"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.operator')" align="center" class-name="small-padding fixed-width" width="140">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleOfflineUpdate(scope.row)"
            v-hasPermi="['device:deviceOfflineTask:edit']"
          >{{$t('common.modify')}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['device:deviceOfflineTask:remove']"
          >{{$t('common.delete')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备离线任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="任务类型">
          <el-radio-group v-model="form.taskType">
            <el-radio  label="offlineTimer" >定时触发</el-radio>
            <el-radio  label="offlineThreshold" >阈值触发</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="监听任务标识" prop="flag">
          <el-input v-model="form.flag" placeholder="请输入监听任务标识" />
        </el-form-item>
        <el-form-item label="执行日期" prop="date" v-if="form.taskType === 'offlineTimer'">
          <el-date-picker clearable v-model="form.date" type="date" value-format="yyyy-MM-dd" placeholder="请选择执行日期" /> &nbsp;&nbsp;
          <el-time-picker v-model="form.datetime" value-format="HH:mm:ss"  placeholder="请选择执行时间" />
        </el-form-item>
        <el-form-item label="数据类型" prop="dataType">
          <el-select v-model="form.dataType" placeholder="请选择数据类型">
            <el-option
              v-for="dict in dict.type.task_data_type"
              :key="dict.id"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="从站地址" prop="slaveAddr">
          <el-input v-model="form.slaveAddr" placeholder="请输入从站地址" />
        </el-form-item>
        <el-form-item label="功能码" prop="funcCode">
          <el-select v-model="form.funcCode" placeholder="请选择功能码">
            <el-option
              v-for="dict in dict.type.task_func_code"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="寄存器地址" prop="dataAddr">
          <el-input v-model="form.dataAddr" placeholder="请输入寄存器地址" />
        </el-form-item>
        <el-form-item label="设置值" prop="dataValue">
          <el-input v-model="form.dataValue" placeholder="请输入值" />
        </el-form-item>
        <el-form-item label="数值变化开启">
          <el-radio-group v-model="form.dataChange">
            <el-radio
              v-for="dict in dict.type.task_enabled"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="数据变化范围" prop="changeValue" v-if="form.dataChange===1">
          <el-input v-model="form.changeValue" placeholder="请输入数据变化范围" />
        </el-form-item>
        <el-form-item label="阈值触发开启">
          <el-radio-group v-model="form.threshold">
            <el-radio
              v-for="dict in dict.type.task_enabled"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="条件" prop="condition" v-if="form.threshold===1">
          <el-select v-model="form.condition" placeholder="请选择条件">
            <el-option
              v-for="dict in dict.type.task_condition"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="小于阈值" prop="thrMin"  v-if="form.threshold===1 && (form.condition == 3 || form.condition == 5)">
          <el-input v-model="form.thrMin" placeholder="请输入小于阈值" />
        </el-form-item>
        <el-form-item label="大于阈值" prop="thrMax"  v-if="form.threshold===1 && (form.condition == 1 || form.condition == 5)">
          <el-input v-model="form.thrMax" placeholder="请输入大于阈值" />
        </el-form-item>
        <el-form-item label="持续时间" prop="delay"  v-if="(form.threshold===1 && form.taskType==='offlineThreshold') || form.taskType==='offlineTimer'">
          <el-input v-model="form.delay" placeholder="请输入持续时间" >
            <el-button slot="append">S</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="结束使能">
          <el-radio-group v-model="form.endEnabled">
            <el-radio
              v-for="dict in dict.type.task_enabled"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="任务结束设置值" prop="endValue" v-if="form.endEnabled===1">
          <el-input v-model="form.endValue" placeholder="请输入任务结束设置值" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDeviceOfflineTask, getDeviceOfflineTask, delDeviceOfflineTask, addDeviceOfflineTask, updateDeviceOfflineTask,offlineTaskConfig } from "@/api/device/deviceOfflineTask";

export default {
  props:{
    deviceId:null
  },
  name: "DeviceOfflineTask",
  dicts: ['task_func_code', 'task_condition', 'task_enabled', 'task_data_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备离线任务表格数据
      deviceOfflineTaskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceId: null,
        taskType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dataValue: [
          { required: true, message: "数据值不能为空", trigger: "blur" }
        ],
        taskType: [
          { required: true, message: "任务类型不能为空", trigger: "change" }
        ],
        dataType: [
          { required: true, message: "数据类型不能为空", trigger: "change" }
        ],
        slaveAddr: [
          { required: true, message: "从站地址不能为空", trigger: "blur" }
        ],
        funcCode: [
          { required: true, message: "功能码不能为空", trigger: "change" }
        ],
        dataAddr: [
          { required: true, message: "寄存器地址不能为空", trigger: "blur" }
        ],
        dataChange: [
          { required: true, message: "数值变化开启不能为空", trigger: "blur" }
        ],
        changeValue: [
          { required: true, message: "数据变化范围不能为空", trigger: "blur" }
        ],
        threshold: [
          { required: true, message: "阈值触发开启不能为空", trigger: "blur" }
        ],
        thrMin: [
          { required: true, message: "小于阈值不能为空", trigger: "blur" }
        ],
        thrMax: [
          { required: true, message: "大于阈值不能为空", trigger: "blur" }
        ],
        delay: [
          { required: true, message: "持续时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.queryParams.deviceId = this.deviceId
    this.getList();
  },
  methods: {
    /** 查询设备离线任务列表 */
    getList() {
      this.loading = true;
      listDeviceOfflineTask(this.queryParams).then(response => {
        this.deviceOfflineTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceId: this.deviceId,
        // taskType: 'offlineThreshold',
        flag: null,
        date: null,
        datetime: null,
        dataType: null,
        slaveAddr: null,
        funcCode: null,
        dataAddr: null,
        dataChange: 0,
        changeValue: null,
        threshold: 0,
        condition: null,
        thrMin: null,
        thrMax: null,
        delay: null,
        dataValue:null ,
        endEnabled: 0,
        endValue: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备离线任务";
    },
    /** 修改按钮操作 */
    handleOfflineUpdate(row) {
      this.reset();
      const id = row.id
      getDeviceOfflineTask(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备离线任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDeviceOfflineTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeviceOfflineTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    //配置离线任务
    handleOfflineTaskConfig(){
      offlineTaskConfig(this.deviceId).then(response => {
        this.$modal.msgSuccess("操作成功");
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备离线任务编号为"' + ids + '"的数据项？').then(function() {
        return delDeviceOfflineTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('device/deviceOfflineTask/export', {
        ...this.queryParams
      }, `deviceOfflineTask_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
